package com.yumei.trader.gym.page.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.core.content.ContextCompat;


import com.yumei.trader.gym.R;
import com.yumei.trader.gym.act.BSAction;

import java.util.ArrayList;

/**
 * 仿真交易成绩交易日志列表适配器
 * Created by Administrator on 2018/10/23.
 */

public class GameRecordAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater inflater;
    private ArrayList<BSAction> dataArr = new ArrayList<>();

    public GameRecordAdapter(Context context) {
        mContext = context;
        inflater = LayoutInflater.from(context);
    }

    public void addItems(ArrayList<BSAction> arr){
        this.dataArr.addAll(arr);
        this.notifyDataSetChanged();
    }

    public void clear(){
        this.dataArr.clear();
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return dataArr.size();
    }

    @Override
    public Object getItem(int position) {
        return dataArr.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @SuppressLint({"InflateParams", "SetTextI18n", "DefaultLocale"})
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;

        if(convertView == null){

            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item_game_record, null);

            viewHolder.itemNo = convertView.findViewById(R.id.item_game_record_no);
            viewHolder.itemType = convertView.findViewById(R.id.item_game_record_type);
            viewHolder.itemBuyPrice = convertView.findViewById(R.id.item_game_record_buy_price);
            viewHolder.itemSellPrice = convertView.findViewById(R.id.item_game_record_sell_price);
            viewHolder.itemProfitRate = convertView.findViewById(R.id.item_game_record_profit_rate);

            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)convertView.getTag();
        }

        BSAction element = dataArr.get(position);

        viewHolder.itemNo.setText("" + (position+1));
        viewHolder.itemType.setText(element.actionType);
        viewHolder.itemBuyPrice.setText(String.format("%.2f", element.buyPrice));
        viewHolder.itemSellPrice.setText(String.format("%.2f", element.sellPrice));

        float rate = 0f;

        if (element.actionType.equals("Long")){
             rate = (element.sellPrice - element.buyPrice) / element.buyPrice;
        }else if (element.actionType.equals("Short")){
            rate = -(element.sellPrice - element.buyPrice) / element.buyPrice;
        }

        if (rate > 0){
            viewHolder.itemBuyPrice.setTextColor(ContextCompat.getColor(mContext, R.color.color_stock_down));
            viewHolder.itemSellPrice.setTextColor(ContextCompat.getColor(mContext, R.color.color_stock_down));
            viewHolder.itemProfitRate.setTextColor(ContextCompat.getColor(mContext, R.color.color_stock_down));
            viewHolder.itemProfitRate.setText("+" + String.format("%.2f", rate * 100) + "%");
        }else if (rate < 0){
            viewHolder.itemBuyPrice.setTextColor(ContextCompat.getColor(mContext, R.color.color_stock_up));
            viewHolder.itemSellPrice.setTextColor(ContextCompat.getColor(mContext, R.color.color_stock_up));
            viewHolder.itemProfitRate.setTextColor(ContextCompat.getColor(mContext, R.color.color_stock_up));
            viewHolder.itemProfitRate.setText("" + String.format("%.2f", rate * 100) + "%");
        }else {
            viewHolder.itemBuyPrice.setTextColor(ContextCompat.getColor(mContext, R.color.color_font_main));
            viewHolder.itemSellPrice.setTextColor(ContextCompat.getColor(mContext, R.color.color_font_main));
            viewHolder.itemProfitRate.setTextColor(ContextCompat.getColor(mContext, R.color.color_font_main));
            viewHolder.itemProfitRate.setText("0.00%");
        }

        return convertView;
    }

    private class ViewHolder{

        TextView itemNo;
        TextView itemType;
        TextView itemBuyPrice;
        TextView itemSellPrice;
        TextView itemProfitRate;
    }
}
